package com.demo.listener;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * @author WuLiangHang
 */
@WebServlet(name = "Demo01Servlet", urlPatterns = "/demo01")
public class Demo01Servlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //通过response 字符输出流 传递给客户端HTML页面数据
        //防止响应乱码 必须在从response中获取到输出流前就要设置
        //先考虑，在JSP中 如何输出JAVA变量
        //如何运行java语句
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        //每一行都输出一次 跟JSP是一样的
        writer.write("<!DOCTYPE html>");
        writer.write("<html lang=\"en\">");
        writer.write("<head>");
        writer.write("    <meta charset=\"UTF-8\">");
        writer.write("    <title>response回传的HTML页面</title>");
        writer.write("</head>");
        writer.write("<body>");
        writer.write("    <h1>现在的时间是:" + new Date() + "</h1>");
        writer.write("</body>");
        writer.write("</html>");
    }
}
